package com.superhelper.common.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.common.domain.DeliverRecord;

/**
 * 
 * @author Administrator
 *
 */

public interface DeliverRecordMapper {

    @Update("update deliver_record set status = 1 ,statusMsg=#{statusMsg},statusChangeAt=current_timestamp where id=#{id} and status < 1")
    void updateStatusWait(@Param("id") Long id, @Param("statusMsg") String statusMsg);

    @Update("update deliver_record set currentTip= #{currentTip}, addTipTimes= #{addTipTimes},addTipAt=current_timestamp where id=#{id}")
    void updateAfterAddTip(DeliverRecord record);

    @Update("update deliver_record set status = -1 ,statusMsg=#{statusMsg},statusChangeAt=current_timestamp where id=#{id} and status = 1")
    int updateStatusAbnormal(@Param("id") Long id, @Param("statusMsg") String statusMsg);

    /**
     * 严格按照订单状态大小顺序来执行，小状态不能覆盖大状态
     * 
     * @param id
     * @param status
     * @param statusMsg
     * @return
     */
    @Update("update deliver_record set status= #{status},statusMsg = #{statusMsg} ,statusChangeAt = current_timestamp "
            + ", transporterName=ifnull(transporterName,#{transporterName}),transporterPhone=ifnull(transporterPhone,#{transporterPhone}) where id=#{id} and status <= #{status}")
    int updateStatusByStatusOrder(@Param("id") Long id, @Param("status") Integer status,
            @Param("statusMsg") String statusMsg, @Param("transporterName") String transporterName,
            @Param("transporterPhone") String transporterPhone);

    @Update("update deliver_record set status= 2,statusMsg = #{statusMsg} ,statusChangeAt = current_timestamp, "
            + " transporterName=ifnull(transporterName,#{transporterName}),transporterPhone=ifnull(transporterPhone,#{transporterPhone}) where id=#{id} and status <= 2")
    int updatePickingById(@Param("id") Long id, @Param("statusMsg") String statusMsg,
            @Param("transporterName") String transporterName, @Param("transporterPhone") String transporterPhone);

    /**
     * 不能覆盖已完成这个订单状态
     * 
     * @param id
     * @param status
     * @param statusMsg
     * @return
     */
    @Update("update deliver_record set status= #{status},statusMsg = #{statusMsg} ,statusChangeAt = current_timestamp where id=#{id} and status != 4")
    int updateStatusNotComplete(@Param("id") Long id, @Param("status") Integer status,
            @Param("statusMsg") String statusMsg);

    /**
     * 这是一个不需要管上一个状态值的更新
     * 
     * @param id
     * @param status
     * @param statusMsg
     */
    @Update("update deliver_record set status = #{status} ,statusMsg=#{statusMsg},statusChangeAt=current_timestamp where id=#{id} and status < 1")
    void updateStatus(@Param("id") Long id, @Param("status") int status, @Param("statusMsg") String statusMsg);

    @Update("update deliver_record set status = #{status} ,statusMsg=#{statusMsg},statusChangeAt=current_timestamp,deliverPlf=#{deliverPlf},deliverFee=#{deliverFee},"
            + "deliverNo = #{deliverNo}, transporterName=#{transporterName},transporterPhone=#{transporterPhone} where id=#{id} and status <= #{status}")
    void updateDeliver(DeliverRecord dr);

    @Select("select * from deliver_record where id = #{value}")
    DeliverRecord getById(Long valueOf);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into deliver_record(id,orderId,extId,auserShopId,orderCategory,deliverPlf,deliverNo,deliverFee,deliverAt,currentTip,perTip,"
            + "maxTip,status,statusMsg,createAt)values(#{id},#{orderId},#{extId},#{auserShopId},#{orderCategory},#{deliverPlf},#{deliverNo},#{deliverFee},#{deliverAt},"
            + "#{currentTip},#{perTip},#{maxTip},#{status},#{statusMsg},#{createAt})")
    int addDeliverRecord(DeliverRecord dr);

    @Select("select * from deliver_record where auserShopId=#{auserShopId} and orderCategory=#{orderCategory} and createAt > #{date} order by id desc limit 1")
    DeliverRecord getLastlyDeliverRecord(@Param("auserShopId") Long auserShopId,
            @Param("orderCategory") Integer orderCategory, @Param("date") Date date);

    @Select("select count(1) from deliver_record where orderId=#{orderId} and extId=#{extId}")
    int hasOrderId(@Param("orderId") Long orderId, @Param("extId") Integer extId);

    /**
     * 发单后，无人接手的 提前两秒取出
     * 
     * @param shopId
     * @param minutes
     * @return
     */
    @Select("select * from deliver_record where status=1 and auserShopId=#{shopId} and addTipTimes=0 and "
            + "statusChangeAt between DATE_ADD(now(),interval -(#{minutes}+10) minute)  and DATE_ADD(now(),interval -(#{minutes}*60-2) second)"
            + " and currentTip+perTip<= maxTip")
    List<DeliverRecord> queryFirstNeedAddTipRecords(@Param("shopId") Long shopId, @Param("minutes") int minutes);

    /**
     * 加小费后，无人接手的 提前两秒取出
     * 
     * @param shopId
     * @param minutes
     * @return
     */
    @Select("select * from deliver_record where status=1 and auserShopId=#{shopId} and addTipTimes > 0 and "
            + "addTipAt between DATE_ADD(now(),interval -(#{minutes}+10) minute) and "
            + " DATE_ADD(now(),interval -(#{minutes}*60-2) second) and currentTip+perTip<= maxTip")
    List<DeliverRecord> queryAfterAddTipNeedMoreRecords(@Param("shopId") Long shopId, @Param("minutes") int minutes);

    @Select("select id from deliver_record where status=1 and addTipAt between DATE_ADD(now(),interval -35 minute) and DATE_ADD(now(),interval -330 second)  and currentTip+perTip>maxTip")
    List<Long> queryAbnormalOrder();

    @Select("select orderId,auserShopId from deliver_record where createAt>date_sub(curdate(),interval 0 day) and status=-1 group by auserShopId")
    List<DeliverRecord> getAbnormalOrderList();

    @Select("select * from deliver_record where orderId=#{value} order by extId desc limit 1")
    DeliverRecord getLastlyByOrderId(Long orderId);

}
